package com.cnc.servlet;

import com.cnc.pojo.User;
import com.cnc.service.Impl.UserServiceImpl;
import com.cnc.service.UserService;
import com.cnc.utils.JsonUtil;
import com.cnc.utils.ResultUtil;
import com.cnc.utils.VerifyCodeUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.ArrayList;

@WebServlet("/user")
public class UserServlet extends BaseServlet{
    UserService us=new UserServiceImpl();

    protected void Login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user = req.getParameter("user");
        String pwd = req.getParameter("pwd");
        String code = req.getParameter("code");
        PrintWriter writer = resp.getWriter();
        HttpSession session = req.getSession();
        String msg = us.Login(user, pwd, code, session);
        writer.print(msg);
        writer.close();
    }


    protected void VerifyCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        //存入会话session
        HttpSession session = req.getSession(true);
        //删除以前的
        session.removeAttribute("verCode");
        session.setAttribute("verCode", verifyCode.toLowerCase());
        //生成图片
        int w = 60, h = 30;
        VerifyCodeUtils.outputImage(w, h, resp.getOutputStream(), verifyCode);
    }

    protected void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String search = req.getParameter("search");
        PrintWriter writer = resp.getWriter();
        writer.print(us.getAdminList(currentPage,pageSize,search));
    }

    protected void prepare(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object user = session.getAttribute("user");
        ResultUtil resultUtil = null;
        if(user==null){
            resultUtil=new ResultUtil(2,null,null);
        }else {
            resultUtil=new ResultUtil(1,null,user);
        }
        String json = JsonUtil.objectToJson(resultUtil);
        PrintWriter writer = resp.getWriter();
        writer.print(json);
        writer.close();
    }
    protected void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        PrintWriter writer = resp.getWriter();
        writer.print("退出成功！");
        writer.close();
    }

    protected void getUserInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        PrintWriter writer = resp.getWriter();
        writer.print(us.getUserInfo(uid));
        writer.close();
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getReader().readLine();
        if(data!=null){
            resp.getWriter().print(us.updateUser(data));
        }
        //resp.getWriter().print(us.updateUser(req.getReader().readLine()));
    }
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getReader().readLine();
        if(data!=null){
            resp.getWriter().print(us.addUser(data));
        }
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }
    protected void delUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getReader().readLine();
        if(data!=null){
            resp.getWriter().print(us.delAllSelect(data));
        }
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String data = req.getReader().readLine();
        if(data!=null){
            resp.getWriter().print(us.register(data));
        }
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }
    protected void checkPhone(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String phone = req.getParameter("phone");
        resp.getWriter().print(us.checkPhone(phone));
        //resp.getWriter().print(us.addUser(req.getReader().readLine()));
    }

    protected void userLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pwd = req.getParameter("pwd");
        String phone = req.getParameter("phone");
        PrintWriter writer = resp.getWriter();
        writer.print(us.userLogin(phone,pwd));
        writer.close();
    }
}
